package com.hydra.modulecenter;

import android.util.Log;

import static com.hydra.modulecenter.ModuleCenter.MC_LOG_TAG;

/**
 * Created by hasee on 2017/7/27.
 *
 */
@SuppressWarnings("unchecked")
class ModuleInfo {

    Class<? extends IModuleInterface> moduleCls;
    Class<? extends IModuleInterface> interfaceCls;
    IModuleInterface moduleInstance;
    boolean isPreLoad;

    void initInstance() {
        try {
            moduleInstance = moduleCls.newInstance();
        } catch (Throwable e) {
            Log.e(MC_LOG_TAG, "init module instance failed " + moduleCls.getName() + " error : " + e.toString());
        }
    }

    <M extends IModuleInterface> M getModuleInstance() {
        if(moduleInstance == null) {
            synchronized (this) {
                if(moduleInstance == null) {
                    if(isPreLoad) {
                        Log.e(MC_LOG_TAG, "module " + interfaceCls.getName() + " is preload failed");
                        return null;
                    }

                    initInstance();
                }
            }
        }

        return (M) moduleInstance;
    }
}
